package com.camera.road.client;

import com.camera.road.sink.Sink;
import com.camera.road.sink.SinkList;
import com.camera.road.sink.Sink.SinkInfo;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.History;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

public class Main extends Composite {

	private static MainUiBinder uiBinder = GWT.create(MainUiBinder.class);

	interface MainUiBinder extends UiBinder<Widget, Main> {
	}

	@UiField HTML message;
	
	@UiField VerticalPanel menuList;
	
	@UiField HTML descript;
	
	@UiField VerticalPanel body;
	
	@UiField HTML copyRight;
	
	@UiField VerticalPanel main;
	
	SinkList list = new SinkList();
	
	private SinkInfo curInfo;
	
	private Sink curSink;

	public Main(String firstName) {
		initWidget(uiBinder.createAndBindUi(this));
		this.setWidth("100%");
		menuList.setWidth("150px");
//		menuList.setBorderWidth(20);
		body.setSize("750px", "600px");
		message.setText("successful");
		addMenuList();
		menuList.add(list);
		main.setStyleName("sink");
		copyRight.setText("Copyright(@)2010 版权所有：成都中联信通科技有限公司 All Right Reserve ");
		
		History.addValueChangeHandler(new ValueChangeHandler<String>() {
			public void onValueChange(ValueChangeEvent<String> event) {
				// Find the SinkInfo associated with the history context. If one
				// is
				// found, show it (It may not be found, for example, when the
				// user mis-
				// types a URL, or on startup, when the first context will be
				// "").
				String token = event.getValue();
				SinkInfo info = list.find(token);
				if (info == null) {
					showInfo();
					return;
				}
				show(info, false);
			}
		});

		// Show the initial screen.
		String initToken = History.getToken();
		if (initToken.length() > 0) {
			onHistoryChanged(initToken);
		} else {
			showInfo();
		}
		
	}
	
	public void onHistoryChanged(String token) {
		// Find the SinkInfo associated with the history context. If one is
		// found, show it (It may not be found, for example, when the user mis-
		// types a URL, or on startup, when the first context will be "").
		SinkInfo info = list.find(token);
		if (info == null) {
			showInfo();
			return;
		}
		show(info, false);
	}
	
	private void addMenuList(){
		list.addSink(HomeSink.init());
		list.addSink(PhotoSink.init());
	}
	
	public void show(SinkInfo info, boolean affectHistory) {
		if (info == curInfo) {
			return;
		}
		curInfo = info;

		// Remove the old sink from the display area.
		if (curSink != null) {
			curSink.onHide();
			body.remove(curSink);
//			sinkContainer.remove(curSink);
		}

		// Get the new sink instance, and display its description in the
		// sink list.
		curSink = info.getInstance();
		list.setSinkSelection(info.getName());
		descript.setText(info.getDescription() + "    ");

		if (affectHistory) {
			History.newItem(info.getName());
		}

		// Display the new sink.
		curSink.setVisible(false);
		body.add(curSink);
//		sinkContainer.add(curSink, DockPanel.CENTER);
//		sinkContainer.setCellHorizontalAlignment(curSink,
//				VerticalPanel.ALIGN_CENTER);
		curSink.setVisible(true);
		curSink.onShow();
	}

	/**
	 * 载入各个sinks的实例，这与每个应用相关，所以在这里用abstract声明，具体载入的实例在其实现类里面去完成。
	 */
//	abstract public void loadSinks();

	// 首先显示Home界面。
	// TODO: Home界面与每个产品相关，是否/怎样实现一个abstract来强制产品实例类来构造实际的Home页面呢？
	// 目前采用各个项目自己构造自己的Home界面来实现这个要求。by ychun.w
	private void showInfo() {
		show(list.find("Home"), false);
	}
}
